package com.aysafety.eventmodel.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class DateUtils {

    private final static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

    private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");

    private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");

    private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取YYYY格式
     */
    public static String getYear() {
        return sdfYear.format(new Date());
    }

    /**
     * 获取YYYY-MM-DD格式
     */
    public static String getDay() {
        return sdfDay.format(new Date());
    }

    /**
     * 获取YYYYMMDD格式
     */
    public static String getDays() {
        return sdfDays.format(new Date());
    }

    /**
     * 获取YYYY-MM-DD hh:mm:ss格式
     */
    public static String getTime() {
        return sdfTime.format(new Date());
    }

    public static String getTime(Date dateTime) {
        return sdfTime.format(dateTime);
    }

    /**
     * @return boolean
     * @Title: compareDate
     * @Description: TODO(日期比较 ， 如果s > = e 返回true 否则返回false)
     * @author luguosui
     */
    public static boolean compareDate(String s, String e) {
        if (fomatDate(s) == null || fomatDate(e) == null) {
            return false;
        }
        return fomatDate(s).getTime() >= fomatDate(e).getTime();
    }

    /**
     * 格式化日期
     */
    public static Date fomatDate(String date) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return fmt.parse(date);
        } catch (ParseException e) {
            logger.error("fomatDate:", e);
            return null;
        }
    }

    /**
     * 格式化日期
     */
    public static Date fomatDate(String date, String format) {
        DateFormat fmt = new SimpleDateFormat(format);
        try {
            return fmt.parse(date);
        } catch (ParseException e) {
            logger.error("fomatDate:", e);
            return null;
        }
    }

    public static Long getFirstSecond(String date, int sec) {
        Long timestamp = (DateUtils.fomatDateTime(date).getTime() / 1000 - sec) * 1000;
        return timestamp;
    }

    public static Long getLastSecond(String date, int sec) {
        Long timestamp = (DateUtils.fomatDateTime(date).getTime() / 1000 + sec) * 1000;
        return timestamp;
    }

    public static Long getCurrentTimeFirstSecond(int sec) {
        Long timestamp = (DateUtils.fomatDateTime(DateUtils.getTime()).getTime() / 1000 - sec) * 1000;
        return timestamp;
    }

    public static Long getCurrentTimeLastSecond(int sec) {
        Long timestamp = (DateUtils.fomatDateTime(DateUtils.getTime()).getTime() / 1000 + sec) * 1000;
        return timestamp;
    }

    /**
     * 格式化日期
     */
    public static Date fomatDateTime(String date) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return fmt.parse(date);
        } catch (ParseException e) {
            logger.error("fomatDateTime:", e);
            return null;
        }
    }

    public static Date fomatDateTime(Long date) {
        try {
            Date dateTemp = new Date(date);
            return dateTemp;
        } catch (Exception e) {
            logger.error("fomatDateTime:", e);
            return null;
        }
    }

    /**
     * 校验日期是否合法
     */
    public static boolean isValidDate(String s) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            fmt.parse(s);
            return true;
        } catch (Exception e) {
            logger.error("isValidDate:", e);
            return false;
        }
    }

    public static int getDiffYear(String startTime, String endTime) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long aa = 0;
            int years =
                (int) (((fmt.parse(endTime).getTime() - fmt.parse(startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
            return years;
        } catch (Exception e) {
            logger.error("getDiffYear:", e);
            return 0;
        }
    }

    /**
     * <li>功能描述：时间相减得到天数
     *
     * @return long
     * @author Administrator
     */
    public static long getDaySub(String beginDateStr, String endDateStr) {
        long day = 0;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd mm");
            Date beginDate = null;
            Date endDate = null;
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
            day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (ParseException e) {
            logger.error("getDaySub:", e);
        }
        return day;
    }

    /**
     * 得到n天之后的日期
     */
    public static String getAfterDayDate(String days) {
        int daysInt = Integer.parseInt(days);
        Calendar canlendar = Calendar.getInstance();
        canlendar.add(Calendar.DATE, daysInt);
        Date date = canlendar.getTime();
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
        return dateStr;
    }

    /**
     * 得到n天之后是周几
     */
    public static String getAfterDayWeek(String days) {
        int daysInt = Integer.parseInt(days);
        Calendar canlendar = Calendar.getInstance();
        canlendar.add(Calendar.DATE, daysInt);
        Date date = canlendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);
        return dateStr;
    }

    /**
     * 根据出生日期计算年龄
     *
     * @return 未来日期返回0
     */
    public static int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            return 0;
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                age--;
            }
        }

        return age;
    }

    /**
     * 根据出生日期计算年龄
     *
     * @param strBirthDay 字符串型日期
     * @param format      日期格式
     * @return 未来日期返回0
     */
    public static int getAge(String strBirthDay, String format) throws Exception {
        DateFormat df = new SimpleDateFormat(format);
        Date birthDay = df.parse(strBirthDay);
        return getAge(birthDay);
    }

    public static String getCurrentTimeFirstSecond(String formatValue, int sec) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - sec);
        return format.format(calendar.getTime());
    }

    public static String getCurrentTimeLastSecond(String formatValue, int sec) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + sec);
        return format.format(calendar.getTime());
    }

    public static String getCurrentStartTimeFirstDay(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentEndTimeFirstDay(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }



    public static String getCurrentTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        return format.format(calendar.getTime());
    }

    public static String getCurrentStartTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentStartTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentMinuteStartTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        calendar.set(Calendar.SECOND, 0);
        return format.format(calendar.getTime());
    }

    /**
     * @Description:小时的开始时间
     * @Param: [formatValue, datetime]
     * @return: java.lang.String
     * @Author: ChengQi
     * @Date: 2019/1/3
     */
    public static String getHoursStartTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    /**
     * @Description: 小时的结束时间
     * @Param: [formatValue, datetime]
     * @return: java.lang.String
     * @Author: ChengQi
     * @Date: 2019/1/3
     */
    public static String getHoursEndTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }

    public static String getCurrentEndTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }

    public static String getCurrentEndTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }


    public static String getCurrentMinuteEndTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        calendar.set(Calendar.SECOND, 59);
        return format.format(calendar.getTime());
    }


    public static String getCurrentMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        return format.format(calendar.getTime());
    }

    public static String getCurrentMonth(String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        return format.format(calendar.getTime());
    }

    public static String getCurrentWeek() {
        SimpleDateFormat format = new SimpleDateFormat("MM-W");
        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        return format.format(calendar.getTime());
    }

    public static String getCurrentWeek(String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("MM-W");
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(tempDatetime);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        return format.format(calendar.getTime());
    }


    public static String getCurrentLastYearStartTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentLastMonthStartTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentLastWeekStartTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 7);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentMonthStartTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentMonthStartTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentMonthEndTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }

    public static String getCurrentMonthEndTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }

    public static String getCurrentYearStartTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, 0);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentYearStartTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        calendar.add(Calendar.YEAR, 0);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentYearEndTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_YEAR, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }

    public static String getCurrentYearEndTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDatetime);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_YEAR, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }

    public static String getCurrentWeekStartTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentWeekStartTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(tempDatetime);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentWeekEndTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }

    public static String getCurrentWeekEndTime(String formatValue, String datetime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Date tempDatetime = format.parse(datetime);
        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(tempDatetime);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }

    public static String getCurrentWeekStartTimePlusWeek(String formatValue, int week) {
        int day = week * 7;
        System.out.println(day);
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, day);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    public static String getCurrentWeekEndTimePlusWeek(String formatValue, int week) {
        int day = week * 7;
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, day);
        calendar.add(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }

    /**
     * @Description: 计算两个时间的天数差
     * @Param: [endDate, nowDate]
     * @return: java.lang.String
     * @Author: ChengQi
     * @Date: 2018/12/20
     */
    public static long getDatePoorByDay(String startTime, String endTime) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = fomatDateTime(endTime).getTime() - fomatDateTime(startTime).getTime();
        long day = diff / nd;
        return day;
    }

    /**
     * 得到n分钟之前的日期
     */
    public static String getBeforeMinuteDate(int daysInt, String current) throws Exception {
        Calendar canlendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = sdf.parse(current);
        canlendar.setTime(d);
        canlendar.add(Calendar.MINUTE, daysInt);
        Date date = canlendar.getTime();
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
        return dateStr;
    }

    /**
     * 得到n分钟之前的日期
     */
    public static String getBeforehourDate(int daysInt, String current) throws Exception {
        Calendar canlendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = sdf.parse(current);
        canlendar.setTime(d);
        canlendar.add(Calendar.HOUR, daysInt);
        Date date = canlendar.getTime();
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
        return dateStr;
    }

    /**
     * 得到n天之前的日期
     */
    public static String getBeforeDayDate(int daysInt, String current, String formatstr) throws Exception {
        Calendar canlendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(formatstr);
        Date d = sdf.parse(current);
        canlendar.setTime(d);
        canlendar.add(Calendar.DATE, daysInt);
        Date date = canlendar.getTime();
        SimpleDateFormat sdfd = new SimpleDateFormat(formatstr);
        String dateStr = sdfd.format(date);
        return dateStr;
    }

    /**
     * @Description: 计算两个时间的分钟差
     * @Param: [endDate, nowDate]
     * @return: java.lang.String
     * @Author: ChengQi
     * @Date: 2018/12/20
     */
    public static long getDatePoorByMinute(String endTime, String startTime) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = fomatDateTime(endTime).getTime() - fomatDateTime(startTime).getTime();
        long min = diff / nm;
        return min;
    }

    /**
     * @Description: 计算两个时间的分钟差
     * @Param: [endDate, nowDate]
     * @return: java.lang.String
     * @Author: ChengQi
     * @Date: 2018/12/20
     */
    public static boolean compretwo(String endTime, String startTime) throws ParseException {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (dateFormat.parse(startTime).getTime() < dateFormat.parse(endTime).getTime()) {
            return true;
        } else {
            return false;
        }
    }



}
